import ala from '@/service/ala.js'
import axios from 'axios'
// export let fileHost = 'http://localhost:5242/' // 文件服务器地址，上传图片地址，上传文件地址，上传视频地址等
// export let fileHost = 'https://img.5ug.top/' // 文件服务器地址，上传图片地址，上传文件地址，上传视频地址等
export let fileHost = 'https://api.alaboshu.com/' // 文件服务器地址，上传图片地址，上传文件地址，上传视频地址等

/**
 * 客户端服务器地址，上传图片地址
 * @returns
 */
export function uploadApi() {
    var apiUrl = fileHost + 'upload'
    return apiUrl
}

/**
 * 文件上传成功处理
 * @param {*} response
 */
export async function uploadSuccess(response) {
    console.info(response, response)
    if (response && response.code === 1) {
        ala.success(response.message)
    } else {
        ala.error(response.message)
    }
}

/**
 * 图片地址处理
 * @param {*} src
 * @returns
 */
export function urlImage(src) {
    if (src) {
        if (src.indexOf('://') === -1) {
            src = fileHost + src
        }
    }
    return src
}


/*
 * 上传复制到剪切板的图片
 * 上传base64的图片，并且返回路径
 */
export async function uploadCopyImage(data) {
    const { file } = data
    var parameter = {
        extension: file.type,
        miniurl: file.miniurl,
        name: file.name,
        size: file.size,
        type: file.type
    }
    var apiUrl = fileHost + 'Api/StorageFile/UploadBase64'
    let request
    await axios.post(apiUrl, parameter, {
        headers: uploadHead()
    }).then(function (res) {
        request = res.data.result
    }).catch(function (error) {
        console.info(error)
    })
    return request
}

/**
 * 图片上传头部
 * @returns
 */
export function uploadHead() {
    var siteId = ala.siteId
    var timestamp = ala.timestamp()
    var headObj = {
        Timestamp: timestamp,
        Token: baseToken(siteId, timestamp),
        Tenant: 'master',
        'Accept-Site-Identity': siteId
    }
    console.log('uploadHead', headObj)
    return headObj
}



function baseToken(siteId) {
    if (!siteId) {
        ala.error('站点Id不能为空')
    }
    var timestamp = ala.timestamp()
    var sign = `${siteId}-${timestamp}-${siteId}`
    var token = ala.md5(sign.toLowerCase())
    return token
}
